﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace XEvents.Library.Internal
{
    internal class EventSessionData : XEventBase
    {
        internal EventSessionData(SqlServer server)
            : base(server)
        {
            LoadSessionDefinitions();
            LoadActiveSessions();
        }

        #region Private Members
        internal List<server_event_session> server_event_sessions;
        internal List<server_event_session_action> server_event_session_actions;
        internal List<server_event_session_event> server_event_session_events;
        internal List<server_event_session_field> server_event_session_fields;
        internal List<server_event_session_target> server_event_session_targets;

        internal List<dm_xe_session> sessions;
        //internal List<dm_xe_session_event_action> session_event_actions;
        //internal List<dm_xe_session_event> session_events;
        //internal List<dm_xe_session_object_column> session_object_columns;
        //internal List<dm_xe_session_target> session_targets;
        #endregion

        #region Private Methods
        private void LoadSessionDefinitions()
        {

            server_event_sessions = new List<server_event_session>();
            server_event_session_actions = new List<server_event_session_action>();
            server_event_session_events = new List<server_event_session_event>();
            server_event_session_fields = new List<server_event_session_field>();
            server_event_session_targets = new List<server_event_session_target>();

            SqlDataReader dr = Server.ServerConnection.ExecuteReader(SqlCommandStrings.SessionDefinitions);

            server_event_session ses;
            server_event_session_action sesa;
            server_event_session_event sese;
            server_event_session_field sesf;
            server_event_session_target sest;

            while (dr.Read())
            {
                ses = new server_event_session(dr);
                this.server_event_sessions.Add(ses);
            }
            dr.NextResult();
            while (dr.Read())
            {
                sesa = new server_event_session_action(dr);
                this.server_event_session_actions.Add(sesa);
            }
            dr.NextResult();
            while (dr.Read())
            {
                sese = new server_event_session_event(dr);
                this.server_event_session_events.Add(sese);
            }
            dr.NextResult();
            while (dr.Read())
            {
                sesf = new server_event_session_field(dr);
                this.server_event_session_fields.Add(sesf);
            }
            dr.NextResult();
            while (dr.Read())
            {
                sest = new server_event_session_target(dr);
                this.server_event_session_targets.Add(sest);
            }
            dr.Close();
        }

        private void LoadActiveSessions()
        {
            sessions = new List<dm_xe_session>();
            //session_event_actions = new List<dm_xe_session_event_action>();
            //session_events = new List<dm_xe_session_event>();
            //session_object_columns = new List<dm_xe_session_object_column>();
            //session_targets = new List<dm_xe_session_target>();

            SqlDataReader dr = Server.ServerConnection.ExecuteReader(SqlCommandStrings.RunningSessions);

            dm_xe_session ses;
            //dm_xe_session_event_action sea;
            //dm_xe_session_event se;
            //dm_xe_session_object_column soc;
            //dm_xe_session_target st;

            while (dr.Read())
            {
                ses = new dm_xe_session(dr);
                this.sessions.Add(ses);
            }
            /*
            dr.NextResult();
            while (dr.Read())
            {
                sea = new dm_xe_session_event_action(dr);
                this.session_event_actions.Add(sea);
            }
            dr.NextResult();
            while (dr.Read())
            {
                se = new dm_xe_session_event(dr);
                this.session_events.Add(se);
            }
            dr.NextResult();
            while (dr.Read())
            {
                soc = new dm_xe_session_object_column(dr);
                this.session_object_columns.Add(soc);
            }
            dr.NextResult();
            while (dr.Read())
            {
                st = new dm_xe_session_target(dr);
                this.session_targets.Add(st);
            }
            */
            dr.Close();
        }
        #endregion


    }
}
